package models

import (
	"time"
	"gorm.io/gorm"
)

// Block 区块模型
type Block struct {
	ID            uint      `json:"id" gorm:"primaryKey"`
	Index         int64     `json:"index" gorm:"uniqueIndex;not null"`
	Hash          string    `json:"hash" gorm:"uniqueIndex;not null"`
	PreviousHash  string    `json:"previous_hash" gorm:"not null"`
	MerkleRoot    string    `json:"merkle_root" gorm:"not null"`
	Nonce         int64     `json:"nonce" gorm:"not null"`
	Difficulty    int       `json:"difficulty" gorm:"not null"`
	Timestamp     time.Time `json:"timestamp" gorm:"not null"`
	CreatedAt     time.Time `json:"created_at"`
	UpdatedAt     time.Time `json:"updated_at"`
	DeletedAt     gorm.DeletedAt `json:"-" gorm:"index"`
	
	// 关联
	Transactions []Transaction `json:"transactions" gorm:"foreignKey:BlockID"`
}

// TableName 指定表名
func (Block) TableName() string {
	return "blocks"
}

// Transaction 交易模型
type Transaction struct {
	ID        uint      `json:"id" gorm:"primaryKey"`
	BlockID   uint      `json:"block_id" gorm:"not null"`
	TxHash    string    `json:"tx_hash" gorm:"uniqueIndex;not null"`
	From      string    `json:"from" gorm:"not null"`
	To        string    `json:"to" gorm:"not null"`
	Amount    string    `json:"amount" gorm:"not null"`
	GasPrice  string    `json:"gas_price" gorm:"not null"`
	GasLimit  uint64    `json:"gas_limit" gorm:"not null"`
	Nonce     uint64    `json:"nonce" gorm:"not null"`
	Data      string    `json:"data" gorm:"type:text"`
	Signature string    `json:"signature" gorm:"not null"`
	Status    string    `json:"status" gorm:"default:'pending'"`
	CreatedAt time.Time `json:"created_at"`
	UpdatedAt time.Time `json:"updated_at"`
	DeletedAt gorm.DeletedAt `json:"-" gorm:"index"`
}

// TableName 指定表名
func (Transaction) TableName() string {
	return "transactions"
}

// CreateTransactionRequest 创建交易请求
type CreateTransactionRequest struct {
	From     string `json:"from" binding:"required"`
	To       string `json:"to" binding:"required"`
	Amount   string `json:"amount" binding:"required"`
	GasPrice string `json:"gas_price"`
	GasLimit uint64 `json:"gas_limit"`
	Data     string `json:"data"`
}

// TransactionResponse 交易响应
type TransactionResponse struct {
	ID        uint   `json:"id"`
	TxHash    string `json:"tx_hash"`
	From      string `json:"from"`
	To        string `json:"to"`
	Amount    string `json:"amount"`
	GasPrice  string `json:"gas_price"`
	GasLimit  uint64 `json:"gas_limit"`
	Nonce     uint64 `json:"nonce"`
	Data      string `json:"data"`
	Status    string `json:"status"`
	CreatedAt string `json:"created_at"`
}

// BlockResponse 区块响应
type BlockResponse struct {
	ID           uint                  `json:"id"`
	Index        int64                 `json:"index"`
	Hash         string                `json:"hash"`
	PreviousHash string                `json:"previous_hash"`
	MerkleRoot   string                `json:"merkle_root"`
	Nonce        int64                 `json:"nonce"`
	Difficulty   int                   `json:"difficulty"`
	Timestamp    string                `json:"timestamp"`
	Transactions []TransactionResponse `json:"transactions"`
	CreatedAt    string                `json:"created_at"`
}

// BlockchainInfo 区块链信息
type BlockchainInfo struct {
	Height           int64  `json:"height"`
	LatestBlockHash  string `json:"latest_block_hash"`
	Difficulty       int    `json:"difficulty"`
	PendingTxs       int    `json:"pending_transactions"`
	TotalWallets     int    `json:"total_wallets"`
	TotalTransactions int   `json:"total_transactions"`
}

// MineBlockRequest 挖矿请求
type MineBlockRequest struct {
	MinerAddress string `json:"miner_address" binding:"required"`
}

// MineBlockResponse 挖矿响应
type MineBlockResponse struct {
	BlockHash    string `json:"block_hash"`
	BlockIndex   int64  `json:"block_index"`
	Transactions int    `json:"transactions_count"`
	MiningTime   string `json:"mining_time"`
}
